package dao;

import javax.swing.plaf.nimbus.State;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
 * BaseDao
 *
 * @author lenovo
 * @since 2023/8/25
 */
public class BaseDao {
    private static String config="library.properties";
    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    /**
     * 静态代码块，只执行一次
     */
    static {
        loadProperties(config);
        loadDriver();
    }

    /**
     * 加载配置文件
     * @param config
     */
    private static void loadProperties(String config){
        Properties properties=new Properties();
        try {
            properties.load(BaseDao.class.getClassLoader().getResourceAsStream(config));
            driver= properties.getProperty("driver");
            url= properties.getProperty("url");
            username= properties.getProperty("username");
            password= properties.getProperty("password");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 加载驱动
     */
    private  static void loadDriver(){
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建连接
     * @return
     */
    public static Connection getConnection(){
        Connection conn=null;
        try {
            conn= DriverManager.getConnection(url,username,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 增删改操作
     * @param sql
     * @param param
     * @return
     */
    public int executeUpdate(String sql,Object...param){
        Connection conn=getConnection();
        PreparedStatement pstmt=null;
        int row=0;
        try {
            pstmt= conn.prepareStatement(sql);
            if(param!=null){
                for(int i=0;i< param.length;i++){
                    pstmt.setObject(1+i,param[i]);
                }
            }
            row=pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(null,pstmt,conn);
        }
        return row;
    }

    public int executeUpdate(String s) {
        Connection conn = getConnection();
        PreparedStatement pstmt = null;
        int result = 0;
        try {
            pstmt = conn.prepareStatement(s);
            System.out.println(s + "------" + pstmt + "-----");
            result = pstmt.executeUpdate();
        } catch (Exception e) {
            System.out.println("执行更新错误！");
        }
        return result;
    }

    public ResultSet executeQuerys(String s) {
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        Connection conn = getConnection();
        try {
            pstmt = conn.prepareStatement(s);
            rs = pstmt.executeQuery();
        } catch (Exception e) {
            System.out.println("执行查询错误！ " + e.getMessage());
        }
        return rs;
    }

    /**
     * 关闭连接
     * @param rs
     * @param pstmt
     * @param conn
     */
    public static void closeAll(ResultSet rs, PreparedStatement pstmt, Connection conn){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }if(pstmt!=null){
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

